test(sentry-client): scope fetch mocks to a per-test URL marker#832
Merged
Conversation
The unit tests installed a raw `globalThis.fetch` mock and asserted a global `callCount`. CI run 24835339085 flaked on `main` with `Received: 7` for a `callCount === 2` assertion — five foreign HTTP calls (org-ID-1 / project `4510776311808000`, i.e. this CLI's own Sentry telemetry project) landed through my mock during the 1 s retry backoff. They were real async work leaked from a prior test that outlived that test's `afterEach` fetch restore, so by the time they resolved they hit whatever `globalThis.fetch` pointed at — my mock. Scope every fetch-mocking test to a unique URL marker (`__test_string_body__`, `__test_request_body__`, …) via `scopedFetchMock`: URLs that don't include the marker are delegated to the captured `originalFetch` (preload.ts blocker in the common case), so foreign calls neither increment this test's counter nor appear in its recorded payloads. 240-round `--rerun-each` run stays green. No production code touched.
Contributor
Codecov Results 📊✅ 138 passed | Total: 138 | Pass Rate: 100% | Execution Time: 0ms 📊 Comparison with Base Branch
✨ No test changes detected All tests are passing successfully. ✅ Patch coverage is 100.00%. Project has 1934 uncovered lines. Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 95.31% 95.32% +0.01%
==========================================
Files 284 284 —
Lines 41337 41341 +4
Branches 0 0 —
==========================================
+ Hits 39401 39407 +6
- Misses 1936 1934 -2
- Partials 0 0 —Generated by Codecov Action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix the flaky
sentry-client.test.tsunit tests — #829's merge commit then tripped on run 24835339085 withexpect(callCount).toBe(2) → Received: 7. The test replacedglobalThis.fetchwith a counting mock and asserted the exact number of calls, but during the 1 s retry backoff window five foreign HTTP calls leaked through —/api/0/organizations/1/,/api/0/projects/1/4510776311808000/, etc., i.e. this CLI's own Sentry telemetry project. They came from async work in a prior test that outlived that test'safterEachfetch restore, so by the time the promises resolved they hit whateverglobalThis.fetchpointed at — my counting mock.Fix
Introduce
scopedFetchMock(marker, handler)that only invokeshandlerfor URLs containing the unique per-test marker (__test_string_body__,__test_request_body__,__test_stream_body__,__test_formdata_body__,__test_user_abort__,___timeout-test___). Any URL without the marker is delegated to the capturedoriginalFetch— which is the preload.ts blocker in the common case, so foreign calls fail fast in their own context without polluting this test'scallCount.No production code touched.
Verification
bun test test/lib/sentry-client.test.ts— 8/8 pass.bun test test/lib/sentry-client.test.ts --rerun-each 30— 240/240 pass.bun test test/lib/sentry-client.test.ts test/lib/api/ test/commands/issue/— 221/221 pass.bunx tsc --noEmit— clean.bun run lint— clean (unrelated pre-existingmarkdown.tswarning).Also simulated the pollution scenario locally by running a parallel test file that fires
setTimeout→globalThis.fetch('https://foreign.example/…')calls duringsentry-client.test.ts— the scoped mock correctly ignores them and the assertions hold.